package cc.chinagps.gateway.unit.eg.upload.cmds;

import cc.chinagps.gateway.log.LogManager;
import cc.chinagps.gateway.unit.UnitServer;
import cc.chinagps.gateway.unit.UnitSocketSession;
import cc.chinagps.gateway.unit.eg.define.EGDefines;
import cc.chinagps.gateway.unit.eg.pkg.EGPackage;
import cc.chinagps.gateway.unit.eg.upload.DeviceInfoManager;
import cc.chinagps.gateway.unit.eg.upload.bean.DeviceLog;
import cc.chinagps.gateway.unit.eg.util.EGTimeFormatUtil;
import cc.chinagps.gateway.util.SingletonFactory;
import org.apache.log4j.Logger;

import java.util.Date;

public class Cmd82 extends CheckLoginHandler {
    private Logger logger_debug = Logger.getLogger(LogManager.LOGGER_NAME_DEBUG);

    @Override
    public boolean handlerPackageSessionChecked(EGPackage pkg, UnitServer server, UnitSocketSession session)
            throws Exception {
        String callLetter = session.getUnitInfo().getCallLetter();
        int pos = 0;
        byte[] data = pkg.getData();
        String collectTime = "20" + cc.chinagps.gateway.util.Util.getDateTime(data, pos, 6);
        pos += 8;
        byte[] info = new byte[data.length - 8];
        System.arraycopy(data, pos, info, 0, info.length);
        String deviceInfo = new String(info, 0, info.length, EGDefines.CHARSET);
        DeviceLog deviceLog = new DeviceLog();
        deviceLog.setDeviceId(callLetter);
        deviceLog.setLogInfo(deviceInfo);
        deviceLog.setCollectTime(EGTimeFormatUtil.parseGMT8(collectTime).getTime());
        deviceLog.setReceiveTime(new Date().getTime());
        logger_debug.debug("[EG][Cmd82][" + callLetter + "]recv deviceLog:" + deviceLog);
        SingletonFactory.getInstance(DeviceInfoManager.class).addLogInfo(deviceLog);
        return true;
    }
}